package com.rainwood.material.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.rainwood.material.R;
import com.rainwood.material.base.BaseActivity;
import com.rainwood.material.bean.DeptBean;
import com.rainwood.material.bean.ScanMaterBean;
import com.rainwood.material.net.NetBean;
import com.rainwood.material.uitls.ToastUtils;
import com.rainwood.material.view.titlebar.OnTitleBarListener;
import com.rainwood.material.view.titlebar.TitleBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther : hhx
 * @date :  2021/3/5
 * @des :
 */
public class CheckOutDetailActivity extends BaseActivity {
    private TitleBar titleBar;
    private NestedScrollView netView;
    private TextView tvMaterialType;
    private TextView tvOneType;
    private TextView tvTwoType;
    private TextView tvMaterCode;
    private TextView tvMaterName;
    private TextView tvMaterModel;
    private TextView tvRemark;
    private EditText etMaterialCount;
    private TextView tvIsReturn;
    private TextView tvAccountName;
    private TextView tvUseWay;
    private EditText etRemark;
    private RelativeLayout rlLayout;
    private Button btnSubmit;
    private ScanMaterBean scanMaterBean;
    private List<DeptBean> deptBeans;
    private int deptOp;
    private int userIdOp;

    private List<String> options1Items = new ArrayList<>();
    private ArrayList<List<String>> options2Items = new ArrayList<>();
    private String optUser;
    private String[] usecase;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_out_detail;
    }

    @Override
    protected void initView() {
        titleBar = (TitleBar) findViewById(R.id.title_bar);


        netView = (NestedScrollView) findViewById(R.id.net_view);
        tvMaterialType = (TextView) findViewById(R.id.tv_material_type);
        tvOneType = (TextView) findViewById(R.id.tv_one_type);
        tvTwoType = (TextView) findViewById(R.id.tv_two_type);
        tvMaterCode = (TextView) findViewById(R.id.tv_mater_code);
        tvMaterName = (TextView) findViewById(R.id.tv_mater_name);
        tvMaterModel = (TextView) findViewById(R.id.tv_mater_model);
        tvRemark = (TextView) findViewById(R.id.tv_remark);
        etMaterialCount = (EditText) findViewById(R.id.et_material_count);
        tvIsReturn = (TextView) findViewById(R.id.tv_is_return);
        tvAccountName = (TextView) findViewById(R.id.tv_account_name);
        tvUseWay = (TextView) findViewById(R.id.tv_use_way);
        etRemark = (EditText) findViewById(R.id.et_remark);
        rlLayout = (RelativeLayout) findViewById(R.id.rl_layout);
        btnSubmit = (Button) findViewById(R.id.btn_submit);


        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });

        tvAccountName.setOnClickListener(v -> showAccountDialog());

        tvIsReturn.setOnClickListener(v -> showReTurnDialog());

        tvUseWay.setOnClickListener(v -> {
            showUseWayDialog();
        });

        btnSubmit.setOnClickListener(v -> {
            submitData();
        });
    }

    /**
     * quantity	是	string	领用数量
     * isReturn	是	int	是否归还(1:是;0:否)
     * id	是	string	物料id
     * stid	是	string	领用用户id
     * purpose	是	string	用途
     * remarks	否	string	备注
     */
    private void submitData() {
        if (TextUtils.isEmpty(etMaterialCount.getText().toString().trim())) {
            ToastUtils.show("请输入领用数量");
            return;
        }
        if (TextUtils.isEmpty(tvAccountName.getText().toString())) {
            ToastUtils.show("请选择领用账户");
            return;
        }
        if (TextUtils.isEmpty(tvIsReturn.getText().toString())) {
            ToastUtils.show("请选择是否归还");
            return;
        }
        if (TextUtils.isEmpty(tvUseWay.getText().toString())) {
            ToastUtils.show("请选择用途");
            return;
        }

        String stid = "";
        for (int i = 0; i < deptBeans.size(); i++) {
            if (deptBeans.get(i).getName().equals(tvAccountName.getText().toString()) && deptBeans.get(i).getDepartmentName().equals(options1Items.get(deptOp))) {
                stid = deptBeans.get(i).getStid();
                break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("quantity", etMaterialCount.getText().toString());
        map.put("isReturn", tvIsReturn.getText().toString().equals("是") ? 1 : 0);
        map.put("id", scanMaterBean.getId());
        map.put("stid", stid);
        map.put("purpose", tvUseWay.getText().toString());
        map.put("remarks", etRemark.getText().toString().trim());

        addPostRequest(NetBean.ADDOUT, map);
    }

    private void showUseWayDialog() {
        if (usecase == null || usecase.length == 0) {
            return;
        }
        List<String> items = new ArrayList<>();
        for (int i = 0; i < usecase.length; i++) {
            items.add(usecase[i]);
        }
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                tvUseWay.setText(items.get(options1));
            }
        }).build();
        pvOptions.setPicker(items);
        pvOptions.setTitleText("用途");
        pvOptions.setSelectOptions(deptOp);
        pvOptions.show();

    }

    private void showReTurnDialog() {
        List<String> items = new ArrayList<>();
        items.add("是");
        items.add("否");
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                tvIsReturn.setText(items.get(options1));
            }
        }).build();
        pvOptions.setPicker(items);
        pvOptions.setTitleText("是否归还");
        pvOptions.setSelectOptions(deptOp);
        pvOptions.show();
    }

    private void showAccountDialog() {
        if (deptBeans == null || deptBeans.isEmpty()) {
            return;
        }


        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                deptOp = options1;
                userIdOp = options2;
                optUser = options2Items.size() > 0
                        && options2Items.get(options1).size() > 0 ?
                        options2Items.get(options1).get(options2) : "";
                tvAccountName.setText(optUser);

            }
        }).build();
        pvOptions.setPicker(options1Items, options2Items);//三级选择器
        pvOptions.setSelectOptions(deptOp, userIdOp);
        pvOptions.setTitleText("账户名称");
        pvOptions.show();


    }

    @Override
    protected void initData() {
        String code = getIntent().getStringExtra("code");
        getMaterialDetail(code);
        getDeptList();
        getUserCase();
    }

    private void getUserCase() {
        Map<String, Object> map = new HashMap<>(1);
        addPostRequest(NetBean.USECASE, map);
    }

    private void getDeptList() {
        Map<String, Object> map = new HashMap<>(1);
        addPostRequest(NetBean.DEPTUSERLIST, map);
    }

    private void getMaterialDetail(String code) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no", code);
        addPostRequest(NetBean.MATERIALDETIAL, map);
    }

    @Override
    public void onSuccess(String res, int tag) {
        if (tag == NetBean.MATERIALDETIAL.getTag()) {
            scanMaterBean = JSON.parseObject(res, ScanMaterBean.class);
            if (scanMaterBean != null) {
                fillTopView(scanMaterBean);
            }
        } else if (tag == NetBean.DEPTUSERLIST.getTag()) {
            JSONObject jsonObject = JSON.parseObject(res);
            deptBeans = JSON.parseArray(jsonObject.getString("department"), DeptBean.class);
            options1Items.clear();
            options2Items.clear();
            for (int i = 0; i < deptBeans.size(); i++) {
                if (options1Items.contains(deptBeans.get(i).getDepartmentName())) continue;
                options1Items.add(deptBeans.get(i).getDepartmentName());
                List<String> items = new ArrayList<>();
                items.clear();
                for (int j = 0; j < deptBeans.size(); j++) {
                    if (deptBeans.get(i).getDepartmentName().equals(deptBeans.get(j).getDepartmentName())) {
                        items.add(deptBeans.get(j).getName());
                    }
                }
                options2Items.add(items);
            }
        } else if (tag == NetBean.USECASE.getTag()) {
            JSONObject jsonObject = JSONObject.parseObject(res);
            String paValue = jsonObject.getString("paValue");
            if (!TextUtils.isEmpty(paValue)) {
                usecase = paValue.split("，");
            }
        } else if (tag == NetBean.ADDOUT.getTag()) {
            ToastUtils.show("领用成功");
            startActivity(new Intent(this,OperateSuccessActivity.class));
        }

    }

    @Override
    public void onFail(String res, int tag) {

    }

    private void fillTopView(ScanMaterBean scanMaterBean) {
        tvMaterialType.setText(scanMaterBean.getMaterialType());
        tvOneType.setText(scanMaterBean.getFirstClassification());
        tvTwoType.setText(scanMaterBean.getSecondClassification());
        tvMaterCode.setText(scanMaterBean.getNo());
        tvMaterName.setText(scanMaterBean.getName());
        tvMaterModel.setText(scanMaterBean.getSize());
        tvRemark.setText(scanMaterBean.getRemarks());
    }

    @Override
    public void onError(String msg) {
        ToastUtils.show(msg);
    }
}
